//
// Created by bleedingfight on 2020/6/29.
//
#include "maxProfit.h"
int maxProfit(std::vector<int>& prices) {
    int size = prices.size();
    int max_profit = 0;
    if(size<=1)
        return 0;
    else{
        int cache=0;
        int min_value = prices[0];
        for(int i=1;i<size;i++){
            if(prices[i]<min_value)
                min_value = prices[i];
            cache = std::max(cache,prices[i]-min_value);
        }
        max_profit = cache;
    }
    return max_profit;
}
int maxProfit1(std::vector<int>& prices){
    int size = prices.size();
    int max_profit = 0;
    if(size<=1)
        return 0;
    else{
        int min_value = prices[0];
        int max_value = prices[0];
        int cache = 0;
        int i = 0;
        while (i<size-1){
            while (i<size-1&&prices[i]>=prices[i+1])//寻找后一个元素大于前一个元素的位置（波谷）
                i++;
            min_value=prices[i];
            while (i<size-1&&prices[i]<=prices[i+1])//寻找后一个元素大于前一个元素的位置（波峰）
                i++;
            max_value = prices[i];
            max_profit += max_value-min_value;
        }
    }
    return max_profit;
}
